package com.hudongwx.godoffood.fragment.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.home_activity.FoodClassificationActivity;
import com.hudongwx.godoffood.activity.home_activity.TitleWebActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hudongwx on 16-11-5.
 */
public class MyGridViewAdapter extends BaseAdapter {
    JSONArray data;
    Activity activity;
    FoodItemOnclickListener listener;

    public MyGridViewAdapter(JSONArray data, Activity activity) {
        this.data = data;
        this.activity = activity;
        listener = new FoodItemOnclickListener();
    }

    @Override
    public int getCount() {
        return data.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder=null;
        //设置复用
        if (null == convertView){
            viewHolder = new MyViewHolder();
            convertView = activity.getLayoutInflater().inflate(R.layout.food_menu_item,null);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.menu_item_iv);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.menu_item_tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        try {
            //设置图片和文字
            x.image().bind(viewHolder.iv,data.getJSONObject(position).getString("IconUrl"));
            viewHolder.tv.setText(data.getJSONObject(position).getString("Alias"));

            viewHolder.iv.setTag(position);
            //设置点击事件
            viewHolder.iv.setOnClickListener(listener);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }


    class MyViewHolder{
        ImageView iv;
        TextView tv;
    }

    class FoodItemOnclickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(activity, FoodClassificationActivity.class);

            intent.putExtra("type", (Integer) v.getTag());
            activity.startActivity(intent);
        }
    }
}
